%% Filename : npwm.m
function [out] = npwm_v1(in, Res, edgeType, bThreeLevel)

%% Edge type definitions
ET_TAIL_EDGE    = 0;
ET_LEAD_EDGE    = 1;
ET_DOUBLE_EDGE  = 2;

%% Oversampling
OSR         = 2^Res;
Nbits_in    = length(in);
L           = Nbits_in/OSR;
out         = zeros(1, Nbits_in,'single');

%% generate saw_tooth mask
switch(edgeType)
    case ET_TAIL_EDGE
        saw_tooth   = linspace(-1,1,OSR);
    case ET_LEAD_EDGE
        saw_tooth   = linspace(1,-1,OSR);
    case ET_DOUBLE_EDGE
        saw_tooth   = linspace(1, -1, OSR/2+1);
        saw_tooth   = [saw_tooth(1:OSR/2+1) linspace(-1+2/OSR, 1-2/OSR, OSR/2-1)];
end

%% npwm
%% generate PWM
for i=1:L
     out((i-1)*OSR+1:i*OSR) = in((i-1)*OSR+1:i*OSR) > saw_tooth;
end